package com.telenav.serverproxy.impl;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import com.telenav.comm.Comm;
import com.telenav.comm.Host;
import com.telenav.comm.HostProvider;
import com.telenav.serverproxy.AbstractJsonServerProxy;
import com.telenav.serverproxy.ICityServerProxy;
import com.telenav.serverproxy.IServerProxyConstants;
import com.telenav.serverproxy.IServerProxyListener;

public class CityServerProxy extends AbstractJsonServerProxy implements ICityServerProxy{

	private double lat;
	
	private double lon;
	
	public CityServerProxy(Host host, Comm comm,
			IServerProxyListener serverProxyListener) {
		super(host, comm, serverProxyListener); 
	}

	@Override
	protected void parseRequestSpecificData(String action, JSONObject jsonObject)
			throws JSONException {
		if (IServerProxyConstants.ACT_GET_CITY.equals(action))
        {
           
        }
	}

	@Override
	public void reverseCityGeocode(double lat, double lon) {
		this.lat = lat;
		this.lon = lon;
		try {
			this.sendRequest(null, IServerProxyConstants.ACT_GET_CITY);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	protected Host getHostByAction(String action)
    {
        if(action.equalsIgnoreCase(IServerProxyConstants.ACT_GET_CITY))
		{
			Host host = new Host();
			host.protocol = Host.HTTP;
	        host.host = HostProvider.MAP_API_URL +lat + "," + lon + "&key=" + HostProvider.API_KEY;
			return host;
		}	
        
        return super.getHostByAction(action);
    }

	@Override
	protected void parseRequestSpecificData(JSONObject jsonObject)
			throws JSONException {
		// TODO Auto-generated method stub
		if (jsonObject.getString("status").equals("OK")) {
			JSONObject json = jsonObject.getJSONObject("result").getJSONObject("addressComponent");
			String city = json.getString("city");
		}
	}

}
